package craky.keeper;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.border.EmptyBorder;

import craky.component.JImagePane;
import craky.componentc.JCButton;
import craky.componentc.JCDialog;
import craky.componentc.JCLabel;
import craky.componentc.JCMessageBox;
import craky.componentc.JCTextArea;
import craky.layout.LineLayout;
import craky.util.UIUtil;

public class AboutDialog extends JCDialog implements ActionListener {
	private static final long serialVersionUID = 3367067393044735726L;
	private KeeperApp keeper;
	private JComponent content;
	private JCButton btnOk;

	public AboutDialog(KeeperApp keeper) {
		super(keeper.getMainFrame(), "关于三号管家",
				Dialog.ModalityType.DOCUMENT_MODAL);
		this.keeper = keeper;
		getContentPane().setPreferredSize(new Dimension(300, 180));
		initUI();
		setLocationRelativeTo(getOwner());
		setVisible(true);
	}

	private void initUI() {
		String bannerName = this.keeper.getBannerName();
		this.content = ((JComponent) getContentPane());
		JImagePane bannerPane = new JImagePane();
		this.content.setPreferredSize(new Dimension(338, 225));
		this.content.setLayout(new BorderLayout());
		this.content.setBorder(new EmptyBorder(0, 2, 2, 2));
		bannerPane.setPreferredSize(new Dimension(-1, 64));
		bannerPane.setMode("Tiled");

		if (bannerName != null) {
			Image image = new ImageIcon(KeeperConst.BANNER_DIR
					+ KeeperConst.FILE_SEP + bannerName).getImage();
			bannerPane.setImage(image);
		}

		this.content.add(bannerPane, "North");
		this.content.add(createMainPane(), "Center");
		setDefaultCloseOperation(2);
		pack();
		setResizable(false);
		setLocationRelativeTo(getOwner());
		UIUtil.escAndEnterAction(this, this.btnOk, new AbstractAction() {
			private static final long serialVersionUID = -8727069883429572261L;

			public void actionPerformed(ActionEvent e) {
				AboutDialog.this.dispose();
			}
		});
	}

	private JImagePane createMainPane() {
		JImagePane mainPane = new JImagePane();
		JCLabel lbVersion = new JCLabel("三号管家(NO.3 Keeper) 1.0.3");
		JCLabel lbCopyright = new JCLabel(
				"Copyright© 2010-2011 003. All Rights Reserved");
		JCTextArea txtInfo = new JCTextArea();
		mainPane.setBackground(new Color(255, 255, 255, 150));
		mainPane.setLayout(new LineLayout(0, 6, 0, 4, 6, 11, 11, 1));
		lbVersion.setHorizontalAlignment(2);
		lbVersion.setVerticalAlignment(1);
		lbVersion.setPreferredSize(new Dimension(-1, 21));
		lbVersion.setBorder(null);
		lbCopyright.setHorizontalAlignment(2);
		lbCopyright.setVerticalAlignment(1);
		lbCopyright.setPreferredSize(lbVersion.getPreferredSize());
		lbCopyright.setBorder(null);
		lbCopyright.setFont(new Font("Tahoma", 0, 12));
		txtInfo.setBorder(null);
		txtInfo.clearBorderListener();
		txtInfo.setImageOnly(true);
		txtInfo.setEditable(false);
		txtInfo.setPopupMenuEnabled(false);
		txtInfo.setFocusable(false);
		txtInfo.setCursor(new Cursor(0));
		txtInfo.setText("本软件完全免费，禁止用于任何商业用 ，个人用户基于学习\n和研究目的的传播、修改将不受限制 ");

		mainPane.add(lbVersion, "StartFill");
		mainPane.add(createAuthorComponent(), "StartFill");
		mainPane.add(lbCopyright, "StartFill");
		mainPane.add(txtInfo, "MiddleFill");
		mainPane.add(createButton(), "End");
		return mainPane;
	}

	private JComponent createAuthorComponent() {
		EmptyComponent ecAuthor = new EmptyComponent();
		JCLabel lbAuthor = new JCLabel("作�1�7�：");
		JCLabel lbMail = new JCLabel("003");
		ecAuthor.setPreferredSize(new Dimension(-1, 21));
		ecAuthor.setLayout(new LineLayout(0, 10, 10, 0));
		lbAuthor.setHorizontalAlignment(2);
		lbAuthor.setVerticalAlignment(1);
		lbAuthor.setBorder(null);
		lbMail.setHorizontalAlignment(2);
		lbMail.setVerticalAlignment(1);
		lbMail.setBorder(null);
		lbMail.setForeground(new Color(22, 112, 235));
		lbMail.setCursor(new Cursor(12));
		lbMail.setToolTipText("xyzt_003@163.com");
		ecAuthor.add(lbAuthor, "StartFill");
		ecAuthor.add(lbMail, "StartFill");
		UIUtil.actionLabel(lbMail, new AbstractAction() {
			private static final long serialVersionUID = -8727069883429572261L;

			public void actionPerformed(ActionEvent e) {
				AboutDialog.this.mailToAuthor(e);
			}
		});
		return ecAuthor;
	}

	private JComponent createButton() {
		EmptyComponent ecButton = new EmptyComponent();
		this.btnOk = new JCButton("确定(O)");
		ecButton.setPreferredSize(new Dimension(79, 21));
		ecButton.setLayout(new BorderLayout());
		this.btnOk.setPreferredSize(new Dimension(73, 21));
		this.btnOk.setMnemonic('O');
		this.btnOk.addActionListener(this);
		ecButton.add(this.btnOk, "West");
		return ecButton;
	}

	private void mailToAuthor(ActionEvent e) {
		String mailTo = ((JCLabel) e.getSource()).getToolTipText();
		try {
			Desktop.getDesktop().mail(new URI("mailto:" + mailTo));
		} catch (Exception ex) {
			String message = "打开邮件客户端错误，请登陆web邮箱联系作 ：" + mailTo;
			JCMessageBox.createErrorMessageBox(this, getTitle(), message)
					.open();
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.btnOk) {
			dispose();
		}
	}
}